package com.nexgo.payment.scan;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.nexgo.oaf.apiv3.SdkResult;
import com.nexgo.oaf.apiv3.device.scanner.OnScannerListener;
import com.nexgo.oaf.apiv3.device.scanner.Scanner;
import com.nexgo.oaf.apiv3.device.scanner.ScannerCfgEntity;
import com.nexgo.payment.App;
import com.nexgo.payment.R;
import com.nexgo.payment.api.ApiDeviceEngine;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.ui.BaseActivity;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 扫码支付
 * Created by wanghongyang on 2017/9/1.
 */
public class ScanCameraActivity extends BaseActivity {
    private Logger mLog = LoggerFactory.getLogger(ScanCameraActivity.class.getSimpleName());
    Scanner mScanner ;
    public static final int SCAN_REQUEST_CODE = 170;
    public static final int SCAN_RESULT_SUCCESS_CODE = 171;
    public static final int SCAN_RESULT_ERROR_CODE = 172;
    public static final String RESULT_MSG = "result_msg";
    private Intent rIntent = new Intent();
    private boolean isResult  = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transaction);
        mScanner = ApiDeviceEngine.getInstance().getScanner(App.sContext);
        try {
            ScannerCfgEntity cfgEntity = new ScannerCfgEntity();
            cfgEntity.setAutoFocus(true);
            cfgEntity.setBulkMode(false);
            cfgEntity.setUsedFrontCcd(false);
            mScanner.initScanner(cfgEntity, mInitScannerListener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onScanDataResult(boolean isSuccess,String data){
        if(isResult){
            finish();
           return;
        }
        isResult = true;
        int resultCode = SCAN_RESULT_ERROR_CODE;
        if(isSuccess){
            resultCode = SCAN_RESULT_SUCCESS_CODE;
        }
        rIntent.putExtra(RESULT_MSG,data);
        setResult(resultCode,rIntent);
        mScanner.stopScan();
        finish();
    }


    /**
     * 初始化扫码回调
     */
    private OnScannerListener mInitScannerListener = new OnScannerListener() {
        @Override
        public void onInitResult(int retCode) {
            mLog.debug("初始化扫码，返回状态：{}", retCode);
            if (retCode != SdkResult.Success) {
                onScanDataResult(false,"初始化扫码失败");
                return;
            }
            int result = mScanner.startScan(SysConstant.TIME_OUT_60, mStartScannerListener);
            mLog.debug("启动扫码，返回状态：{}", result);
            if (result != SdkResult.Success) {
                onScanDataResult(false,"启动扫码失败");
            }
        }

        @Override
        public void onScannerResult(int retCode, String data) {
        }
    };

    public void onErrCode(int retCode) {
        String errorMsg;
        switch (retCode) {
            case SdkResult.TimeOut:
                errorMsg = App.sContext.getString(R.string.scan_time_out);
                break;
            case SdkResult.Scanner_Customer_Exit:
                errorMsg = "扫码取消";
                break;
            default:
                errorMsg = App.sContext.getString(R.string.scan_fail);
                break;
        }
        onScanDataResult(false,errorMsg);
    }
    /**
     * 扫码结果回调
     */
    private OnScannerListener mStartScannerListener = new OnScannerListener() {

        @Override
        public void onInitResult(int retCode) {
        }

        @Override
        public void onScannerResult(int retCode, String data) {
            mLog.debug("扫码结果，返回状态：{}", retCode);
            if (retCode != SdkResult.Success) {
                onErrCode(retCode);
                return;
            }
            mLog.debug("扫码返回数据：{}", data);
            if(TextUtils.isEmpty(data)){
                onScanDataResult(false,"未扫描到数据");
                return;
            }
            onScanDataResult(true,data);

        }
    };
    public static Intent newInstance(Context context) {
        Intent intent = new Intent(context, ScanCameraActivity.class);
        return intent;
    }

}
